package org.phalanx.client.mapeditor;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;

import org.phalanx.client.UI.swing.PhalanxMapView;
import org.phalanx.client.mapeditor.editortools.MapSelectionMatrix;
import org.phalanx.map.MapCoordinates;
import org.phalanx.map.PhalanxMap;

public class PhalanxMapEditorView extends PhalanxMapView {
	private MapSelectionMatrix selectionMatrix;
	private Color rectangleColor = Color.RED;
	private boolean drawBuildPoints = true;

	public PhalanxMapEditorView(PhalanxMap map) {
		super(map);
	}

	@Override
	protected void postMapDrawing() {
		if (getDrawBuildPoints()) {
			drawBuildPoints();
		}

		// Rectangle Selection should always be last
		drawSelectionRectangle();
	}

	public void drawSelectionRectangle() {
		if (getSelectionArea() != null) {
			Graphics2D g = (Graphics2D) this.graphicsBuffer.getGraphics();

			int pxStart = this.getPixelPointx(getSelectionArea()
					.getSelectionMatrixStart().getX());
			int pyStart = this.getPixelPointy(getSelectionArea()
					.getSelectionMatrixStart().getY());
			int pxEnd = this.getPixelPointx(getSelectionArea()
					.getSelectionMatrixEnd().getX())
					+ this.getTileSize();
			int pyEnd = this.getPixelPointy(getSelectionArea()
					.getSelectionMatrixEnd().getY())
					+ this.getTileSize();
			int pxWidth = Math.abs(pxEnd - pxStart);
			int pxLength = Math.abs(pyEnd - pyStart);

			if (getSelectionArea().getSelectionMatrixStart().getX() <= this
					.getMap().getMapWidth() - 1
					&& getSelectionArea().getSelectionMatrixStart().getY() <= this
							.getMap().getMapLength() - 1) {
				Color revertColor = g.getColor();

				g.setColor(this.rectangleColor);

				/*
				 * if (pxStart > pxEnd && pyStart > pxEnd){ int tempVar; tempVar
				 * = pxStart; pxStart = pxEnd; pxEnd = tempVar;
				 * 
				 * tempVar = pyStart; pyStart = pyEnd; pyEnd = tempVar; }
				 */

				g.drawRect(pxStart, pyStart, pxWidth, pxLength);
				g.drawRect(pxStart + 1, pyStart + 1, pxWidth - 2, pxLength - 2);
				g.drawRect(pxStart + 2, pyStart + 2, pxWidth - 4, pxLength - 4);

				g.setColor(revertColor);
			}
		}
	}

	public void drawBuildPoints() {
		Graphics2D g = (Graphics2D) this.graphicsBuffer.getGraphics();

		Color oldColor = g.getColor();
		g.setColor(Color.white);

		ArrayList<MapCoordinates> buildPointList = getMap().getBuildPoints();

		for (int i = 0; i < buildPointList.size(); i++) {
			Integer pX = this.getPixelPointx(buildPointList.get(i).getX());
			Integer pY = this.getPixelPointy(buildPointList.get(i).getY());

			for (int j = 0; j < 5; j++) {
				// g.drawLine(pX, pY, pX + this.getTileSize(), pY +
				// this.getTileSize());
				/*
				 * g.drawLine(pX, pY + j, pX + this.getTileSize(), pY +
				 * this.getTileSize() + j); g.drawLine(pX + this.getTileSize(),
				 * pY - j, pX, pY + this.getTileSize() - j);
				 */
				g.drawOval(pX + j, pY + j, this.getTileSize() - (j * 2), this
						.getTileSize()
						- (j * 2));
			}
		}

		g.setColor(oldColor);
	}

	public void setSelectionArea(MapSelectionMatrix m) {
		if (m.getSelectionMatrixStart().getX() <= this.getMap().getMapWidth() - 1
				&& m.getSelectionMatrixStart().getY() <= this.getMap()
						.getMapLength() - 1) {

			selectionMatrix = m;
			this.repaint();
		}
	}

	public MapSelectionMatrix getSelectionArea() {
		return selectionMatrix;
	}

	public void setDrawBuildPoints(boolean drawBP) {
		this.drawBuildPoints = drawBP;
	}

	public boolean getDrawBuildPoints() {
		return this.drawBuildPoints;
	}
}
